package com.xm;

import com.alibaba.fastjson.JSON;
import com.xm.bean.Element;
import com.xm.bean.OpenSwagger;
import com.xm.bean.PostWrapper;
import com.xm.bean.ReBody;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class YapiParse {

    public static void main(String[] args) throws IOException {
        String s = fileToString2("C:\\Users\\zs\\Downloads\\swaggerApi (7).json");
        OpenSwagger openSwagger = JSON.parseObject(s, OpenSwagger.class);


        Map<String, Map<String, PostWrapper>> paths = openSwagger.getPaths();
        for (Map.Entry<String, Map<String, PostWrapper>> entry : paths.entrySet()) {
            String url = entry.getKey();
            Map<String, PostWrapper> value = entry.getValue();
            PostWrapper postWrapper = value.get("post");
            if (null == postWrapper) {
                System.out.println("\n==================== postWrapper is null ====================\n" + url);
                continue;
            }
            System.out.println("====================接口路径:" + url + "=====================");
            System.out.println("====================接口名:" + postWrapper.getSummary() + "=====================");
            List<ReBody> parameters = postWrapper.getParameters();
            if (null == parameters || parameters.size() != 1) {
                System.out.println("-------预料之外的数据-------" + url);
                continue;
            }

            ReBody reqBody = parameters.get(0);
            Element schema = reqBody.getSchema();
            if (null == schema) {
                System.out.println("\n=========schema is null =========\n" + url);
                continue;
            }

            System.out.println("=============reqBody================");
            for (Element element1 : schema.floatElement(schema)) {
                if (null != element1.getFileName()) {
                    System.out.println(printTable(element1));
                }

            }

            System.out.println("=============resBody================");

            Map<String, ReBody> responses = postWrapper.getResponses();
            ReBody resBody = responses.get("200");
            if (null == resBody) {
                System.out.println("-------预料之外的数据-------" + url);
                continue;
            }
            Element schema1 = resBody.getSchema();
            for (Element element1 : schema1.floatElement(schema1)) {
                if (null != element1.getFileName()) {
                    System.out.println(printTable(element1));
                }
            }
        }
    }

    private static String fileToString2(String path) throws IOException {
        FileReader reader = new FileReader(new File(path));
        StringBuilder stringBuilder = new StringBuilder();
        char[] buffer = new char[1024];
        int size;
        while ((size = reader.read(buffer)) != -1) {
            stringBuilder.append(buffer, 0, size);
        }
        return stringBuilder.toString();
    }

    private static String printTable(Element element) {
        int level = element.getLevel() - 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < level; i++) {
            sb.append(" ");
        }

        String fileName = element.getFileName();
        String description = null == element.getDescription() ? " " : element.getDescription();
        String type = typeMap(element.getType());
        boolean optional = element.isOptional();

        return sb.append(fileName).append("\t\t\t\t").append(description).append("\t").append(type).append("\t").append(optional ? "Y" : "N").toString();
    }


    private static String typeMap(String type) {
        if (null == type) {
            return "";
        }
        switch (type) {
            case "object":
                return "Map";
            case "number":
                return "Double";
            case "array":
                return "List";
            case "string":
                return "String";
            case "boolean":
                return "Boolean";
            case "integer":
                return "Integer";
            default:
                return "";
        }
    }
}
